﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class MenuItems_old : System.Web.UI.Page
    {
        private void GetMenuItems(string valueOwner)
        {
            //GPSTrackingDBV2.Data.MenuItemsQuery MenuItemsQuery = new GPSTrackingDBV2.Data.MenuItemsQuery();
            
            //GPSTrackingDBV2.Data.ISortColumnCollection obj = GPSTrackingDBV2.Data.ISortColumnCollection;

            //MenuItemsQuery.AppendEquals(GPSTrackingDBV2.Entities.MenuItemsColumn.ValueOwner, valueOwner.ToString());
            //MenuItemsGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.MenuItemsProvider.Find(MenuItemsQuery);//GPSTrackingDBV2.Data.DataRepository.MenuItemsProvider.GetAll();//.GetByCompanyID(objInfoShared.CompanyID);
        }

        private void GetMenuItems()
        {
            //GPSTrackingDBV2.Data.MenuItemsQuery MenuItemsQuery = new GPSTrackingDBV2.Data.MenuItemsQuery();

            //MenuItemsQuery.AppendIsNull(GPSTrackingDBV2.Entities.MenuItemsColumn.ValueOwner);
            //MenuGrid.DataSource = GPSTrackingDBV2.Data.DataRepository.MenuItemsProvider.Find(MenuItemsQuery);//GPSTrackingDBV2.Data.DataRepository.MenuItemsProvider.GetAll();//.GetByCompanyID(objInfoShared.CompanyID);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                HttpContext.Current.Session["CompanyID"] = getSessionManager().CompanyIDSelectedComboBox;

                if (!Page.IsPostBack)
                {
                    /*GridSortExpression expression = new GridSortExpression();
                    
                    expression.FieldName = GPSTrackingDBV2.Entities.MenuItemsColumn.Description.ToString();
                    expression.SetSortOrder("Ascending");
                    this.MenuGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                    expression.FieldName = GPSTrackingDBV2.Entities.MenuItemsColumn.Description.ToString();
                    expression.SetSortOrder("Ascending");
                    this.MenuItemsGrid.MasterTableView.SortExpressions.AddSortExpression(expression);*/
                }

            }
            catch (Exception ex)
            {
                showErrorMessage("No se pudo cargar la pantalla.");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
            }
        }

        protected void MenuGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetMenuItems();
            ResizeGrid();
        }

        protected void MenuItemsGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            GetMenuItems(getMenuGridValueSelected());
            ResizeGrid();
        }

        protected void MenuGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetMenuItems(getMenuGridValueSelected());
            MenuItemsGrid.DataBind();
            ResizeGrid();
        }

        protected string getMenuGridValueSelected()
        {

            try
            {
                foreach (Telerik.Web.UI.GridDataItem selectedItem in MenuGrid.SelectedItems)
                {
                    return selectedItem["Value"].Text;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return "-1";
        }

        private void ResizeGrid()
        {
            decimal Height = Convert.ToDecimal(HiddenFieldHeight.Value) / 2;

            MenuGrid.Height = Unit.Parse(Convert.ToString(Height));
            MenuItemsGrid.Height = Unit.Parse(Convert.ToString(Height));
        }

        protected void lbClearMenuFilters_Click(object sender, EventArgs e)
        {
            MenuGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in MenuGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
                else
                {
                    if (column is GridCheckBoxColumn)
                    {
                        GridCheckBoxColumn boundCheckColumn = column as GridCheckBoxColumn;
                        boundCheckColumn.CurrentFilterValue = "true";
                    }
                }
            }
            clearMenuItemsGrid();
            MenuGrid.MasterTableView.Rebind();
            ResizeGrid();
        }

        protected void lbClearMenuItemsFilters_Click(object sender, EventArgs e)
        {
            MenuItemsGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in MenuItemsGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
                else
                {
                    if (column is GridCheckBoxColumn)
                    {
                        GridCheckBoxColumn boundCheckColumn = column as GridCheckBoxColumn;
                        boundCheckColumn.CurrentFilterValue = "true";
                    }
                }
            }
            MenuItemsGrid.MasterTableView.Rebind();
            ResizeGrid();
        }

         /*private GPSTrackingDBV2.Entities.MenuItems Invoke_Entity(Hashtable Entity)
        {
           GPSTrackingDBV2.Entities.MenuItems MenuItemsEntity = new GPSTrackingDBV2.Entities.MenuItems();
            try
            {
                MenuItemsEntity.ItemId = int.Parse(Entity["ItemID"].ToString());
                MenuItemsEntity.Value = Entity["Value"].ToString();
                if (Entity["ValueOwner"] != null)
                    MenuItemsEntity.ValueOwner = Entity["ValueOwner"].ToString();
                else
                    MenuItemsEntity.ValueOwner = null;
                MenuItemsEntity.Description = Entity["Description"].ToString();
                MenuItemsEntity.IsUser = bool.Parse(Entity["IsUser"].ToString());
                MenuItemsEntity.IsAdmin = bool.Parse(Entity["IsAdmin"].ToString());
                MenuItemsEntity.IsMaster = bool.Parse(Entity["IsMaster"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return MenuItemsEntity;
        }*/

        protected void MenuGrid_InsertCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            //insertMenuItem(e, true);
        }

        protected void MenuItemsGrid_InsertCommand(object sender, GridCommandEventArgs e)
        {
            //insertMenuItem(e, false);
        }

        /*protected void insertMenuItem(Telerik.Web.UI.GridCommandEventArgs e, bool isMenu)
        {
            //GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();
            try
            {
                //tm.BeginTransaction();
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Registro = new Hashtable();
                item.ExtractValues(Registro);

                Registro["ItemID"] = GPSTrackingDBV2Custom.Data.DataRepository.MenuItemsProvider.GetNextID();

                GPSTrackingDBV2.Entities.MenuItems MenuItemsEntity = Invoke_Entity(Registro);
                if(isMenu)
                    MenuItemsEntity.ValueOwner = null;
                else
                    MenuItemsEntity.ValueOwner = getMenuGridValueSelected();

                GPSTrackingDBV2.Data.DataRepository.MenuItemsProvider.Insert(MenuItemsEntity);
                //tm.Commit();
            }
            catch (Exception ex)
            {
                //tm.Rollback();
                showErrorMessage("No se pudo registrar la opción");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
            }
        }*/

        protected void MenuGrid_UpdateCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            updateMenuItem(e);
        }

        protected void MenuItemsGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            updateMenuItem(e);
        }

        protected void updateMenuItem(Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                /*GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Registro = new Hashtable();
                item.ExtractValues(Registro);

                GPSTrackingDBV2.Entities.MenuItems MenuItemsEntity = Invoke_Entity(Registro);

                MenuItemsEntity.OriginalItemId = MenuItemsEntity.ItemId;

                GPSTrackingDBV2.Data.DataRepository.MenuItemsProvider.Update(MenuItemsEntity);*/
            }
            catch (Exception ex)
            {
                showErrorMessage("No se pudo actualizar la opción");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
            }
        }

        protected void MenuGrid_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            deleteMenuItem(e);
        }

        protected void MenuItemsGrid_DeleteCommand(object sender, GridCommandEventArgs e)
        {
            deleteMenuItem(e);
        }

        protected void deleteMenuItem(Telerik.Web.UI.GridCommandEventArgs e)
        {
            //GPSTrackingDBV2.Data.TransactionManager tm = GPSTrackingDBV2.Data.DataRepository.Provider.CreateTransaction();
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Registro = new Hashtable();
                item.ExtractValues(Registro);

                //tm.BeginTransaction();
                //GPSTrackingDBV2.Entities.MenuItems MenuItemsEntity = Invoke_Entity(Registro);

                //GPSTrackingDBV2.Data.DataRepository.MenuItemsProvider.Delete(MenuItemsEntity);

                //tm.Commit();
            }
            catch (Exception ex)
            {
                //tm.Rollback();
                showErrorMessage("No se pudo Eliminar la opción");
                HiddenFieldError.Value = ClassManager.CustomExceptionManager.Generate_Error(ex.Message);
            }
        }

        protected void MenuGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.SortCommandName || e.CommandName == RadGrid.FilterCommandName)
            {
                clearMenuItemsGrid();
            }
        }

        private void clearMenuItemsGrid()
        {
            MenuItemsGrid.DataSource = new string[] { };
            MenuItemsGrid.DataBind();
            ResizeGrid();
        }

        protected void setSessionManager(string value)
        {
            SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];
            objSessionManager.CompaniesByMaster_companySearch_MasterUsername = value;
            HttpContext.Current.Session["SessionManager"] = objSessionManager;
        }

        protected SessionManager getSessionManager()
        {
            return (SessionManager)HttpContext.Current.Session["SessionManager"];
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }

        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Error", "objWindowsMessageManager.show_Error('" + msg + "','Resumen de Rutas...');", true);
        }

        public void showInfoMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Info", "objWindowsMessageManager.show_Info('" + msg + "','Resumen de Rutas...');", true);
        }

        public void showWarningMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_Warning", "objWindowsMessageManager.show_Warning('" + msg + "','Resumen de Rutas...');", true);
        }
    }
}